package xyz.app.domain.addresses;

import lombok.*;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import xyz.app.DaoRegistry;
import xyz.app.Dao;
import xyz.app.dependencies.MemberAccessor;
import xyz.app.Wrapper;
import xyz.app.dependencies.HasDomainMembers;
import xyz.app.dependencies.HasOwners;
import xyz.app.dependencies.OwnerEntry;
import xyz.app.domain.parties.AbstractParty;
import xyz.app.domain.parties.AbstractParty_;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;


@Slf4j
@Service
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class DaoLegalAddress implements Dao<LegalAddress>,
        HasOwners<LegalAddress>,
        HasDomainMembers<LegalAddress> {

    @Getter @Accessors(fluent = true)
    private final DaoRegistry daoRegistry;

    private final Collection<OwnerEntry<?, LegalAddress>> ownersMapping = new HashSet<>();

    @Getter @Accessors(fluent = true)
    private final Collection<MemberAccessor> memberAccessors = Set.of(
            MemberAccessor.of(LegalAddress_.ADDRESS, Wrapper::address, Wrapper::address));

    public Collection<OwnerEntry<?, LegalAddress>> ownersMapping() {

        if (ownersMapping.isEmpty()) daoRegistry
                .ownerEntry(AbstractParty_.legalAddress, AbstractParty.class)
                .ifPresent(ownersMapping::add);

        return ownersMapping;
    }

    @Override
    public @NonNull Class<LegalAddress> domainClass() {
        return LegalAddress.class;
    }
}